﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using AjaxChatService.Models;
using AjaxChatService.YahooMessenger;

namespace AjaxChatService.Controllers
{
    [HttpHeader("Access-Control-Allow-Origin", "*")]
    public class YMMessagesController : ApiController
    {
        public static int Seq = -1;
        public HttpResponseMessage PostMessage(YMMessage message)
        {
            try
            {
                string sessionId = AuthParameters.SessionId;
                string credentials = AuthParameters.Credentials;
                var helper = new MessageHelper();
                helper.ExecuteSendRequest(sessionId, credentials, message.Content, message.Receiver);

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            catch (Exception)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }           
        }

        public IEnumerable<YMMessage> GetMessage()
         {
            string sessionId = AuthParameters.SessionId;
            string credentials = AuthParameters.Credentials;
            var helper = new MessageHelper();
            return helper.ExecuteGetRequest(sessionId, credentials, Seq);
        }
    }
}
